<?php

require_once 'models/UserManager.php';
require_once 'controllers/Controller.php';

class UserController extends Controller
{
	private $_um;
	
	public function __construct() {
		parent::__construct();
		$this->_um = new UserManager($this->_bdd);
	}
	
	public function connexion() {
		$connected = false;
		
		/* Si on recoit des données en POST */
		if(isset($_POST['email']) && isset($_POST['pass'])) {
			$u = $this->_um->get($_POST['email']); // on récupère l'objet user
			/* Si les pass correspondent */
			if($u && ($u->getPassword() == sha1($_POST['pass']))) {
				$_SESSION['email'] = $u->getEmail(); // on attribue la session login
                $_SESSION['phone'] = $u->getPhone();
                header("Location: index.php");
			}
			else {
				$connected = 2;	
			}
		}
		include 'views/connect.php';
	}
	
	public function deconnexion() {
		session_destroy();
		header("Location: index.php");
	} 
	
	public function inscription() {
        $register = false;
        if(isset($_POST['email']) && isset($_POST['pass']) && isset($_POST['pass2']) && isset($_POST['phone'])) {
            $erreur = 0;
            if($_POST['pass'] != $_POST['pass2']) {
                $erreur = 1;
            }
            if($this->_um->exists($_POST['email'])) {
                $erreur = 2;
            }
            else {
                $array = array('email' => $_POST['email'], 'password' => sha1($_POST['pass']), 'phone' => $_POST['phone']);
                $u = new User($array);
                $this->_um->add($u);
            }
            $register = true;
        }
        include 'views/register.php';
	}
	
	public function liste() {

	}
	
	public function maj() {

	}
}

?>